using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using AvaloniaUiKit.Enums;

namespace AvaloniaUiKit.Controls
{
    /// <summary>
    /// 自定义按钮控件
    /// </summary>
    public class MyCustomButton : Button
    {
        #region 依赖属性

        /// <summary>
        /// 定义图标属性
        /// </summary>
        public static readonly StyledProperty<IImage?> IconProperty =
            AvaloniaProperty.Register<MyCustomButton, IImage?>(nameof(Icon));

        /// <summary>
        /// 图标
        /// </summary>
        public IImage? Icon
        {
            get => GetValue(IconProperty);
            set => SetValue(IconProperty, value);
        }

        /// <summary>
        /// 定义角度属性
        /// </summary>
        public static readonly StyledProperty<CornerRadius> CornerRadiusProperty =
            AvaloniaProperty.Register<MyCustomButton, CornerRadius>(nameof(CornerRadius), new CornerRadius(4));

        /// <summary>
        /// 角度
        /// </summary>
        public CornerRadius CornerRadius
        {
            get => GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty, value);
        }

        /// <summary>
        /// 定义按钮状态属性
        /// </summary>
        public static readonly StyledProperty<ControlState> ButtonStateProperty =
            AvaloniaProperty.Register<MyCustomButton, ControlState>(nameof(ButtonState), ControlState.Normal);

        /// <summary>
        /// 按钮状态
        /// </summary>
        public ControlState ButtonState
        {
            get => GetValue(ButtonStateProperty);
            set => SetValue(ButtonStateProperty, value);
        }

        #endregion
        
        protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
        {
            base.OnPropertyChanged(change);
            
            if (change.Property == IsEnabledProperty)
            {
                ButtonState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
            }
        }
        
        protected override void OnPointerEntered(Avalonia.Input.PointerEventArgs e)
        {
            base.OnPointerEntered(e);
            
            if (IsEnabled)
            {
                ButtonState = ControlState.Hover;
            }
        }
        
        protected override void OnPointerExited(Avalonia.Input.PointerEventArgs e)
        {
            base.OnPointerExited(e);
            
            if (IsEnabled)
            {
                ButtonState = ControlState.Normal;
            }
        }
        
        protected override void OnPointerPressed(Avalonia.Input.PointerPressedEventArgs e)
        {
            base.OnPointerPressed(e);
            
            if (IsEnabled)
            {
                ButtonState = ControlState.Pressed;
            }
        }
        
        protected override void OnPointerReleased(Avalonia.Input.PointerReleasedEventArgs e)
        {
            base.OnPointerReleased(e);
            
            if (IsEnabled)
            {
                ButtonState = ControlState.Hover;
            }
        }
    }
} 